package com.excel

import com.languages.entity.StyleValue
import org.apache.poi.hssf.usermodel.*
import org.apache.poi.ss.usermodel.HorizontalAlignment
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.io.InputStream

class SaveExcel {
    private val factory: HSSFCreationHelper

    //第五步插入数据
    var wb: HSSFWorkbook
        private set
    private var style: HSSFCellStyle

    constructor() {
        wb = HSSFWorkbook()
        style = wb.createCellStyle()
        style.setAlignment(HorizontalAlignment.CENTER) //居中
        factory = wb.creationHelper
    }

    constructor(s: InputStream?) {
        wb = HSSFWorkbook(s)
        style = wb.createCellStyle()
        style.setAlignment(HorizontalAlignment.CENTER) //居中
        factory = wb.creationHelper
    }

    /**
     * 创建sheet
     *
     * @param title
     */
    fun addSheet(title: String?): HSSFSheet {
        val sheet = wb.createSheet(title)
        return sheet
    }

    /**
     * 创建行row:添加表头0行
     * @param sheet
     * @param arge
     */
    fun addHead(sheet: HSSFSheet, vararg arge: Any): HSSFRow {
        val headComments = mutableListOf<SheetHeadComment>()
        for (i in arge.indices) {
            if (arge[i] is SheetHeadComment) {
                headComments.add(arge[i] as SheetHeadComment)
            } else {
                headComments.add(SheetHeadComment(arge[i] as String))
            }
        }
        return this.addHead(sheet, *headComments.toTypedArray<SheetHeadComment>())
    }

    fun addHead(sheet: HSSFSheet, vararg arge: SheetHeadComment): HSSFRow {
        // 创建单元格， 行
        val row = sheet.createRow(0)
        var cell: HSSFCell
        // 得到一个换图的对象
        val drawing = sheet.createDrawingPatriarch()
        var sheetHeadComment: SheetHeadComment
        for (i in arge.indices) {
            sheetHeadComment = arge[i]
            // 创建单元格对象，在指定列创建
            cell = row.createCell(i)
            cell.setCellStyle(style) //内容居中
            // 插入单元格内容
            cell.setCellValue(sheetHeadComment.content) //设定值
            //创建绘图对象
            if (!sheetHeadComment.comment.isNullOrBlank()) {
                addCellComment(cell, sheetHeadComment)
            }
        }
        return row
    }

    @JvmOverloads
    fun addCellComment(cell: HSSFCell, sheetHeadComment: SheetHeadComment, author: String? = "bo ge") {
        // 得到一个换图的对象
        val drawing = cell.row.sheet.createDrawingPatriarch()
        // 前四个参数是坐标点,后四个参数是编辑和显示批注时的大小.
        val anchor = HSSFClientAnchor(
            sheetHeadComment.dx1, sheetHeadComment.dy1,
            sheetHeadComment.dx2, sheetHeadComment.dy2,
            sheetHeadComment.col1, sheetHeadComment.row1,
            sheetHeadComment.col2, sheetHeadComment.row2
        )
        val c = drawing.createCellComment(anchor)
        c.string = factory.createRichTextString(sheetHeadComment.comment)
        c.author = author
        cell.cellComment = c
    }

    /**
     * 创建单元格并且添加数据
     * @param sheet
     * @param args
     */
    fun addCellValue(sheet: HSSFSheet, vararg args: String?): HSSFRow {
        val row = addCellValue(sheet)
        for (i in args.indices) {
            row.createCell(i).setCellValue(args[i])
        }
        return row
    }

    /**
     * 创建单元格并且添加数据
     * @param hssfRow
     * @param args
     */
    fun addCellValue(hssfRow: HSSFRow, vararg args: String?): HSSFCell {
        val row = addCellValue(hssfRow)
        for (i in args.indices) {
            row.setCellValue(args[i])
        }
        return row
    }

    /**
     * 创建单元格并且添加数据
     * @param hssfRow
     * @param args
     */
    fun addCellValue(hssfRow: HSSFRow, vararg args: StyleValue): HSSFCell {
        val row = addCellValue(hssfRow)
        for (i in args.indices) {
            row.setCellValue(args[i].content)
            if (args[i].cellStyle != null) row.setCellStyle(args[i].cellStyle)
        }
        return row
    }

    /**
     * 创建单元格并且添加数据
     * @param hssfRow
     */
    fun addCellValue(hssfRow: HSSFRow): HSSFCell {
        return hssfRow.createCell(hssfRow.physicalNumberOfCells)
    }

    /**
     * 创建一行单元格
     * @param sheet
     */
    fun addCellValue(sheet: HSSFSheet): HSSFRow {
        return sheet.createRow(sheet.physicalNumberOfRows)
    }

    /**
     * 将生成excel文件保存到指定路径下
     */
    fun save(path: String) {
        val file = File(path)
//        if (file.isAbsolute) {// 判断是否是绝对路径
//            if(!file.getParentFile().exists()) file.getParentFile().mkdirs()
//        }
        var parent: File
        if ((file.parentFile.also { parent = it }) != null) { // 判断是否是绝对路径
            if (!parent.exists()) parent.mkdirs()
        }
        val fout = FileOutputStream(file)
        wb.write(fout)
        fout.close()
    }

    //        List<Statistics> list = ExportErrorExcel.getErrorCondition();
    //        for (int i = 0; i < list.size(); i++) {
    //            Statistics errorCondition = list.get(i);
    //            //创建行
    //            row = sheet.createRow(i+1);
    //            //创建单元格并且添加数据
    //            row.createCell(0).setCellValue(errorCondition.getName());
    //            row.createCell(1).setCellValue(errorCondition.getIdCard());
    //            row.createCell(2).setCellValue(errorCondition.getStatus());
    //            row.createCell(3).setCellValue(errorCondition.getMessage());
    //        }
}